<?php

/**
 * @author     Thibaut Cromphaut, Gaïtano Boeraeve, Matthias Mercy
 * @copyright  Copyright (c) 2011 memorygame
 */
class WorldController extends Zend_Controller_Action
{

    public function init()
    {
        $this->stylesBase = $this->view->baseUrl() . '/styles/';
    }
    
    public function preDispatch()
    {
        if ( Zend_Auth::getInstance()->hasIdentity() ) {
            switch ( $this->getRequest()->getActionName() ) {
                case 'index':
                    break;                
                case 'edit':
                    break;
                case 'delete':
                    break;
                default:
                    $this->_helper->redirector('index', 'index');
                    break;
            }
        } else {
            switch ( $this->getRequest()->getActionName() ) {
                case 'logout':
                    $this->_helper->redirector('index');
                    break;
                default:
                    break;
            }
        } 
    }
    
    public function indexAction()
    {   
        $view = $this->_getView('Worlds');  
        $worlds = $this->_getAllWorlds();  
        $view->worlds = $worlds;
    }
    
    public function editAction()
    {   
        $view = $this->_getView('Edit world');
        $form = $this->_getEditWorldForm();
        
        if ($this->getRequest()->isPost()) 
	{
            $formData  = $this->_request->getPost();
            if ($form->isValid($formData)) 
            {
                $view = $this->_getView('Fout - Toevoegen');  
                
                // Promotion toevoegen 
                $values = $form->getValues();

                $world_values['id'] = $values['world_id'];
                $world_values['name'] = $values['world_name'];
                $world_values['description'] = $values['world_description'];
                //$world_values['pictureUrl'] = $values['world_pictureUrl'];
       
                $world = new Application_Model_World($world_values);
                                
                //UPLOAD THE PICTURE
                    $world_picture = $form->world_pictureUrl->getFileName();
                    //Zend_Debug::dump($world_picture);
                    //die("upload");
                    if(strlen($world_picture))
                    {
                        $upload_location    = $world_picture;
                        $world_extension   = end(explode('.', $world_picture));
                        //$world_location    = "assets/worlds/";
                        $world_location    = "../../CleverSpriteMemoryGame-debug/assets/worlds/";
                        $world_savename    = 'world_'.$world->Id. '.' . $world_extension;
                        $world_savepath    = $world_location.$world_savename;

                        // Manually do the uploading and renaming, as the Zend way seems to fail
                        if(file_exists($world_location))
                        {
                            if(rename($upload_location, $world_savepath))
                                $world->PictureUrl   = $world_savename;
                        }
                    }
                  
                $worldMapper = new Application_Model_WorldMapper();
                $worldMapper->save($world);

                return $this->_helper->redirector('index', 'world');                
            }
        }else{
            $id = (string)$this->getRequest()->getParam('id');
            
            $world = $this->_getViewWorld($id);            
            
            $form->populate(
                array(
                    'world_id'          => $world->Id,
                    'world_name'        => $world->Name,    
                    'world_description' => $world->Description,
                    'world_pictureUrl'  => $world->PictureUrl,
                ));
            
            $view->form = $form; 
        }   
    }
    
    public function deleteAction()
    {
        $id = (string)$this->getRequest()->getParam('id');
        $worldMapper = new Application_Model_WorldMapper();
        $worldMapper->delete($id);
        $this->_helper->redirector('index', 'world');
    }

    /**
     * @param string $title
     * @return Zend_View
     */
    private function _getView($title)
    {
        $view = $this->view;
        $view->title = $title;
        $view->headTitle($view->title, 'prepend');                           
        $view->headLink()->appendStylesheet($this->stylesBase . 'main.css'); 

        return $view;
    }
    
    private function _getEditWorldForm()
    {   
        return new Application_Form_EditWorld(array(
            'action' => $this->view->baseUrl() . '/world/edit/',
            'post'   => 'post',
        ));
    }
    
    /**
     * @return Array
     */
    private function _getAllWorlds()
    {
        $worldMapper = new Application_Model_WorldMapper();
        $worldArray = $worldMapper->fetchAll();                
        //$userHasWorldMapper = new Application_Model_UserHasWorldMapper();             
        //$userHasWorldMapper->save();

        return $worldArray;
    }
    
    /**
     * @return Array
     */
    /*private function _getAllWorlds()
    {
        $worldMapper = new Application_Model_WorldMapper();
        $worldArray = $worldMapper->fetchAll();

        return $worldArray;
    }*/
    
    private function _getViewWorld($id)
    {    
        $worldMapper = new Application_Model_WorldMapper();
        $worldArray = $worldMapper->fetchById($id);
        
        return $worldArray;
    }
}

